package com.cml.infrastructure;

import com.cml.common.dto.entity.Message;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * 姓名：陈茂林
 * 时间：2023/10/18 13:19
 * 描述：
 */
public class ClientQueueManage {
    public static final Map<String, BlockingQueue<Message>> UPLOAD_QUEUE_MANAGER = new HashMap<String, BlockingQueue<Message>>();

    public static final Map<String, BlockingQueue<Message>> SEND_QUEUE_MANAGER = new HashMap<String, BlockingQueue<Message>>();

    public static void createUploadMessageQueue(String key){
        UPLOAD_QUEUE_MANAGER.put(key, new LinkedBlockingQueue());
    }

    public static void createSendMessageQueue(String key){
        SEND_QUEUE_MANAGER.put(key, new LinkedBlockingQueue());
    }

    public static void removeUploadMessageQueue(String key){
        UPLOAD_QUEUE_MANAGER.remove(key);
    }

    public static void removeSendMessageQueue(String key){
        SEND_QUEUE_MANAGER.remove(key);
    }

    public static void pushSendMessageMessage(String key, Message message) throws InterruptedException {
        ClientQueueManage.SEND_QUEUE_MANAGER.get(key).put(message);
    }

    public static void pushUploadMessageMessage(String key, Message message) throws InterruptedException {
        ClientQueueManage.UPLOAD_QUEUE_MANAGER.get(key).put(message);
    }

    public static Message takeUploadMessageMessage(String key) throws InterruptedException {
        return  ClientQueueManage.UPLOAD_QUEUE_MANAGER.get(key).take();
    }

    public static Message pollSendMessageMessage(String key){
        return ClientQueueManage.SEND_QUEUE_MANAGER.get(key).poll();
    }

    public static Message peekSendMessageMessage(String key){
        return ClientQueueManage.SEND_QUEUE_MANAGER.get(key).peek();
    }
}
